package com.hlcl.huaji;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.elcl.activity.BaseActivity;
import com.elcl.network.AppContext;
import com.elcl.util.StringUtils;
import com.elcl.util.thread.ThreadPoolUtils;
import com.elcl.util.viewutils.WindowUtils;
import com.hlcl.huaji.model.Order;
import com.hlcl.huaji.server.NetCode;
import com.hlcl.huaji.server.OrderServer;
import com.hlcl.huaji.view.HomeActivity;

/**
 * Created by lichunlin on 2018/2/28.
 * 加载页
 */

public class SplashActivity extends BaseActivity {

    private WindowUtils windowUtils;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
//                if (AppContext.getIsFirstLogin()){//如果是第一次登陆
////                    openActivty(GuideActivity.class);
//                    openActivty(HomeActivity.class);
//                }else{
                if (null != AppContext.getUid() && 0 != AppContext.getUid()) {
                    openActivty(HomeActivity.class);
                } else {
//                    openActivty(HomeActivity.class);
                    openActivty(LoginActivity.class);
                }
//                }
                finish();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_splash);

        getConfingServer();
        windowUtils = new WindowUtils();
        windowUtils.fullScreen(SplashActivity.this);
        initSplashView();
    }


    //获取配置的server
    private void getConfingServer() {
        OrderServer.getInstance().getConfingServer(netHandler);
    }

    private void initSplashView() {
        ImageView img_splash = (ImageView) findViewById(R.id.img_splash);
        Glide.with(SplashActivity.this).load(R.mipmap.ic_launcher).into(img_splash);

        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(0);
//                    Thread.sleep(2000);
                    handler.sendEmptyMessage(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    protected void praseJson(int reqId, String json) {
        if (reqId == NetCode.ID_CONFING){//获取配置信息
            AppContext.setConfing(json);
        }
    }

}
